package com.blb.mvc.controller;

import com.blb.mvc.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lvtian
 * @date 2022/12/29 14:08
 */
@Controller
public class StudentController {
    @RequestMapping("/addStudent1")
    public String toAddStudent(){
        return "addStudent";
    }

    public static final String DIR = "F:\\image\\";

private List<Student> students = new ArrayList<>();
    @RequestMapping("/addStudent")
    public String addStudent(Student student , Model model , MultipartFile picture) throws IOException {
        System.out.println("接收到"+student);
        students.add(student);
        model.addAttribute("students", students);
        //文件上传处理
        String filename=picture.getOriginalFilename();
        picture.transferTo(new File(DIR, filename));
        return "index";
    }

    @ResponseBody
    @RequestMapping("testAjax")
    public List<Student> testAjax(@RequestBody Student student){
        students.add(student);
        return students;
    }
}
